home *** CD-ROM | disk | FTP | other *** search
- /* --------------------------------------------------------------------------
- * BUMPMAP.CPP
- * --------------------------------------------------------------------------
- * Project : True DX6 bump mapping
- *
- * Purpose : Test bump mapping on Matrox G400
- *
- * --------------------------------------------------------------------------
- */
-
-
- /* --------------------------------------------------------------------------
- *
- * Assumes all textures are 24-bit .BMP files
- *
- * --------------------------------------------------------------------------
- */
-
- #define FORCE_REFERENCERASTERIZER FALSE // Change this to TRUE to use refrast
-
- #define INITGUID
-
- #define WIDTH 640
- #define HEIGHT 480
- #define BPP 32
-
- #define CAMERADISTANCE 20
-
- #define BASEMAPFILENAME "basemap.bmp"
- #define BUMPMAPFILENAME "bumpmap.bmp"
- #define ENVMAPFILENAME "envmap.bmp"
-
- /* --------------------------------------------------------------------------
- * General include files
- */
- #define INITGUID
-
- #include <stdio.h>
- #include <d3d.h>
- #include <ddraw.h>
-
- /* --------------------------------------------------------------------------
- * Global variables
- */
-
- struct MyVertexFormat
- {
- float sx;
- float sy;
- float sz;
- float rhw;
- DWORD color;
- float tu;
- float tv;
- float tu2;
- float tv2;
- } Vertices[4];
-
- // Structure that will hold the display device (HAL) selected
- //
- struct
- {
- GUID guid;
- D3DDEVICEDESC HWdesc;
- D3DDEVICEDESC HELdesc;
- char strDesc[256];
- char name[256];
- bool isHAL;
- } _3DDevice;
-
- HWND hwnd;
- WNDCLASS wc;
-
- // DirectX Stuff
- //
- LPDIRECTDRAW lpDD_temp = NULL;
- LPDIRECTDRAW4 lpDD = NULL;
-
- LPDIRECTDRAWSURFACE4 lpDDSPrimary = NULL;
- LPDIRECTDRAWSURFACE4 lpDDSBack = NULL;
- LPDIRECTDRAWSURFACE4 lpDDSZ = NULL;
-
- LPDIRECTDRAWSURFACE4 lpDDSBaseTex = NULL;
- LPDIRECT3DTEXTURE2 lpBaseTexture = NULL;
-
- LPDIRECTDRAWSURFACE4 lpDDSBumpTex = NULL;
- LPDIRECT3DTEXTURE2 lpBumpTexture = NULL;
-
- LPDIRECTDRAWSURFACE4 lpDDSEnvTex = NULL;
- LPDIRECT3DTEXTURE2 lpEnvTexture = NULL;
-
- LPDIRECTDRAWSURFACE4 lpDDSTempTex = NULL;
- LPDIRECT3DTEXTURE2 lpTempTexture = NULL;
-
- LPDIRECTDRAWSURFACE4 lpDDSTexSystem256 = NULL;
- LPDIRECT3DTEXTURE2 lpTexSystem256 = NULL;
-
- LPDIRECT3D3 lpD3D = NULL;
- LPDIRECT3DDEVICE3 lpD3DDevice = NULL;
- LPDIRECT3DVIEWPORT3 lpD3DViewport = NULL;
-
-
- WORD MouseX, MouseY;
-
- WORD Triangles[6];
-
- FILE *f;
-
- unsigned char *MatroxLogo = NULL;
- unsigned char *Bumpdata = NULL; // Temp buffer for bump map data
- unsigned char *Bumpdata2 = NULL;
- unsigned char *InBuffer, *OutBuffer;
-
-
- bool DoEffect = FALSE;
-
- /* --------------------------------------------------------------------------
- * Actual code starts here
- */
-
- DWORD FLOATtoDWORD( FLOAT f )
- {
- union FLOATDWORD
- {
- FLOAT f;
- DWORD dw;
- };
-
- FLOATDWORD val;
- val.f = f;
- return val.dw;
- }
-
- /* --------------------------------------------------------------------------
- * Function :
- * EnumDeviceCallback
- *
- * Purpose :
- * Callback that enumerates the D3D devices available
- */
- HRESULT WINAPI EnumDeviceCallback( LPGUID lpGuid
- , LPSTR lpDeviceDescription
- , LPSTR lpDeviceName
- , LPD3DDEVICEDESC lpHWDesc
- , LPD3DDEVICEDESC lpHELDesc
- , LPVOID lpContext
- )
- {
- // Keep only hardware devices that support bump mapping
- //
- if( !(lpHWDesc->dwTextureOpCaps & D3DTEXOPCAPS_BUMPENVMAP) )
- return D3DENUMRET_OK;
-
- // Copy driver info
- //
- memcpy( &_3DDevice.guid, lpGuid, sizeof(GUID) );
- strcpy( _3DDevice.strDesc, lpDeviceDescription );
- strcpy( _3DDevice.name, lpDeviceName );
- memcpy( &_3DDevice.HWdesc, lpHWDesc, sizeof(lpHWDesc) );
- _3DDevice.isHAL = TRUE;
-
- // Bail out as soon as we found the HW device with required caps
- //
- return D3DENUMRET_CANCEL;
- }
-
- /* --------------------------------------------------------------------------
- * Function :
- * ChooseDevice
- *
- * Purpose :
- * Enumerates d3d devices and picks the first HAL
- */
- HRESULT ChooseDevice( void )
- {
- HRESULT err;
-
- _3DDevice.isHAL = FALSE;
-
- err = lpD3D->EnumDevices( EnumDeviceCallback, 0 );
- if( err != D3D_OK )
- return err;
-
- if( (_3DDevice.isHAL == FALSE) || FORCE_REFERENCERASTERIZER )
- {
- _3DDevice.guid = IID_IDirect3DRefDevice;
- _3DDevice.isHAL = FALSE;
- }
-
- return D3D_OK;
- }
-
- LRESULT WINAPI WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- // Handle messages
-
- switch (message)
- {
- case WM_MOUSEMOVE:
- MouseX = LOWORD( lParam ); // horizontal position of cursor
- MouseY = HIWORD( lParam ); // vertical position of cursor
- break;
-
- case WM_KEYDOWN:
- switch( wParam )
- {
- case VK_ESCAPE:
- PostMessage( hwnd, WM_CLOSE ,0 ,0 );
- break;
-
- case VK_SPACE:
- DoEffect = !DoEffect;
- break;
-
- default:
- break;
- }
- break;
-
- case WM_CLOSE:
- DestroyWindow( hwnd );
- break;
-
- case WM_DESTROY:
- PostQuitMessage( 0 );
- break;
- }
-
- return DefWindowProc( hWnd, message, wParam, lParam );
- }
-
- /* --------------------------------------------------------------------------
- * Function :
- * Init
- *
- * Purpose :
- * Initializes DD, D3D, a viewport and all associated buffers.
- */
- BOOL Init( void )
- {
- HINSTANCE hInstance;
- DDSURFACEDESC2 ddsd;
- DDSCAPS2 ddscaps;
- D3DVIEWPORT2 viewData;
- HRESULT hr;
- int found = FALSE;
-
- /*************************************************************************************************/
-
- // Set up and register window class
- //
- hInstance = GetModuleHandle( NULL );
- wc.style = CS_NOCLOSE | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
- wc.lpfnWndProc = (WNDPROC)WindowProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInstance;
- wc.hIcon = NULL;
- wc.hCursor = LoadCursor( NULL, IDC_ARROW );
- wc.hbrBackground = NULL;
- wc.lpszMenuName = "TEST";
- wc.lpszClassName = "TEST";
-
- if( !RegisterClass( &wc ) ) { OutputDebugString( "\n\nRegisterClass failed\n\n" ); return FALSE; }
-
- // Create a window
- //
- hwnd = CreateWindowEx( WS_EX_TOPMOST
- , "TEST"
- , "TEST"
- , WS_POPUP
- , 0
- , 0
- , GetSystemMetrics( SM_CXSCREEN )
- , GetSystemMetrics( SM_CYSCREEN )
- , NULL
- , NULL
- , hInstance
- , NULL
- );
-
- if( !hwnd ) { OutputDebugString( "\n\nCould not create the display window\n\n" ); return FALSE; }
-
- /*************************************************************************************************/
-
- // Create DirectDraw object
- //
- hr = DirectDrawCreate( NULL, &lpDD_temp, NULL );
-
- lpDD_temp->QueryInterface(IID_IDirectDraw4, (LPVOID *)&lpDD);
- lpDD_temp->Release();
-
- if( hr != DD_OK ) { OutputDebugString( "Could not create DD object\n" ); return FALSE; }
-
- // Get the D3D interface
- //
- hr = lpDD->QueryInterface( IID_IDirect3D3, (LPVOID *)&lpD3D );
- if( hr != DD_OK ) { OutputDebugString( "\n\nUnable to get D3D interface\n\n" ); return FALSE; }
-
- // Choose the appropriate D3D device
- //
- OutputDebugString( "\n\nChoosing Device\n\n" );
- ChooseDevice();
-
- // Get exclusive access to DD
- //
- hr = lpDD->SetCooperativeLevel( hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN );
- if( hr != DD_OK ) { OutputDebugString("Could not get exclusive | fullscreen" ); return FALSE; }
-
- hr = lpDD->SetDisplayMode( WIDTH, HEIGHT, BPP, 0, 0 );
- if( hr != DD_OK ) { OutputDebugString( "\n\nSetDisplayMode failed!\n\n"); return FALSE; }
-
- // Create flipping surfaces (front+back buffer)
- //
- memset( &ddsd, 0, sizeof( ddsd ) );
- ddsd.dwSize = sizeof( DDSURFACEDESC2 );
- ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
- ddsd.ddsCaps.dwCaps = DDSCAPS_3DDEVICE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
- ddsd.dwBackBufferCount = 2;
- hr = lpDD->CreateSurface( &ddsd, &lpDDSPrimary, NULL );
-
- if( hr != DD_OK ) { OutputDebugString( "Unable to create front/back buffer\n" ); return FALSE; }
-
- /*************************************************************************************************/
-
- // Attach a Z buffer to the back buffer
- //
- ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
- hr = lpDDSPrimary->GetAttachedSurface( &ddscaps, &lpDDSBack );
-
- if( hr != DD_OK ) { OutputDebugString( "Unable to get backbuffer ptr\n" ); return FALSE; }
-
- // Create the Z buffer
- //
- memset( &ddsd, 0, sizeof( ddsd ) );
- ddsd.dwSize = sizeof( DDSURFACEDESC2 );
- ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_PIXELFORMAT;
- ddsd.dwWidth = WIDTH;
- ddsd.dwHeight = HEIGHT;
- ddsd.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
-
- if( _3DDevice.isHAL )
- ddsd.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
- else
- ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
-
- ddsd.ddpfPixelFormat.dwSize = sizeof( DDPIXELFORMAT );
- ddsd.ddpfPixelFormat.dwFlags = DDPF_ZBUFFER;
- ddsd.ddpfPixelFormat.dwZBufferBitDepth = 16;
- ddsd.ddpfPixelFormat.dwZBitMask = 0xffff;
-
- hr = lpDD->CreateSurface( &ddsd, &lpDDSZ, NULL );
- if( hr != DD_OK ) { OutputDebugString( "\n\nUnable to create Z surface\n\n" ); return FALSE; }
-
- // Attach the Z buffer
- //
- hr = lpDDSBack->AddAttachedSurface( lpDDSZ );
- if( hr != DD_OK ) { OutputDebugString( "\n\nUnable to attach Z surface\n\n" ); return FALSE; }
-
- OutputDebugString("\n\nInit Direct3D stuff\n\n");
-
- // Create the D3D device with the guid we selected previously
- //
- OutputDebugString( "\n\nCreating Device\n\n" );
- hr = lpD3D->CreateDevice( _3DDevice.guid, lpDDSBack, &lpD3DDevice, NULL );
- if( hr != DD_OK ) { OutputDebugString( "\n\nUnable to create lpd3dDevice\n\n" ); return FALSE; }
-
- /*************************************************************************************************/
-
- // Setup the viewport
- //
- memset( &viewData, 0, sizeof( D3DVIEWPORT2 ) );
- viewData.dwSize = sizeof( D3DVIEWPORT2 );
- viewData.dwX = 0;
- viewData.dwY = 0;
- viewData.dwWidth = WIDTH;
- viewData.dwHeight = HEIGHT;
- viewData.dvClipX = -1.0f;
- viewData.dvClipY = 1.0f;
- viewData.dvClipWidth = 2.0f;
- viewData.dvClipHeight = 2.0f;
- viewData.dvMinZ = 0.0f;
- viewData.dvMaxZ = 1.0f;
-
- // Create viewport
- //
- OutputDebugString( "\n\nCreating Viewport\n\n" );
- hr = lpD3D->CreateViewport( &lpD3DViewport, NULL );
- if( hr != DD_OK ) { OutputDebugString( "\n\nCreateViewport failed\n\n" ); return FALSE; }
-
- // Add viewport to device
- //
- OutputDebugString( "\n\nAdding Viewport\n\n" );
- hr = lpD3DDevice->AddViewport( lpD3DViewport );
- if( hr != D3D_OK ) { OutputDebugString( "\nAddViewport failed\n\n" ); return FALSE; }
-
- OutputDebugString( "\n\nSetting Viewport2\n\n" );
- hr = lpD3DViewport->SetViewport2( &viewData );
- if( hr != D3D_OK ) { OutputDebugString( "\n\nSetViewport2 failed\n\n" ); return FALSE; }
-
- OutputDebugString("Setting Current Viewport\n");
- hr = lpD3DDevice->SetCurrentViewport(lpD3DViewport);
- if( hr != D3D_OK ) { OutputDebugString( "\n\nSetCurrentViewport failed\n\n" ); return FALSE; }
-
- /*************************************************************************************************/
-
- // Create a texture surface in system memory (scratch texture used to load)
- //
- memset( &ddsd, 0, sizeof( ddsd ) );
- ddsd.dwSize = sizeof( DDSURFACEDESC2 );
- ddsd.dwFlags = DDSD_WIDTH
- | DDSD_HEIGHT
- | DDSD_CAPS
- | DDSD_PIXELFORMAT
- ;
-
- ddsd.ddpfPixelFormat.dwSize = sizeof( DDPIXELFORMAT );
- ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
- ddsd.ddpfPixelFormat.dwFourCC = 0;
- ddsd.ddpfPixelFormat.dwRGBBitCount = 32;
- ddsd.ddpfPixelFormat.dwRBitMask = 0x00FF0000;
- ddsd.ddpfPixelFormat.dwGBitMask = 0x0000FF00;
- ddsd.ddpfPixelFormat.dwBBitMask = 0x000000FF;
- ddsd.ddpfPixelFormat.dwRGBAlphaBitMask = 0;
-
- ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE
- | DDSCAPS_SYSTEMMEMORY
- ;
-
- ddsd.dwWidth = 256;
- ddsd.dwHeight = 256;
- hr = lpDD->CreateSurface( &ddsd, &lpDDSTexSystem256, NULL );
- hr = lpDDSTexSystem256->QueryInterface( IID_IDirect3DTexture2, (LPVOID *) &lpTexSystem256 );
-
- // Create the bump map surface.
- //
- memset( &ddsd, 0, sizeof(ddsd) );
-
- ddsd.dwSize = sizeof(ddsd);
- ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
- ddsd.dwWidth = 256;
- ddsd.dwHeight = 256;
- ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
-
- if( _3DDevice.isHAL )
- ddsd.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
- else
- ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
-
- ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
- ddsd.ddpfPixelFormat.dwFlags = DDPF_BUMPDUDV;
- ddsd.ddpfPixelFormat.dwBumpBitCount = 16;
- ddsd.ddpfPixelFormat.dwBumpDuBitMask = 0x000000ff;
- ddsd.ddpfPixelFormat.dwBumpDvBitMask = 0x0000ff00;
- ddsd.ddpfPixelFormat.dwBumpLuminanceBitMask = 0x00000000;
-
- hr = lpDD->CreateSurface( &ddsd, &lpDDSBumpTex, NULL );
- hr = lpDDSBumpTex->QueryInterface( IID_IDirect3DTexture2, (VOID**)&lpBumpTexture );
-
-
- // Set a couple of default renderstates
- //
- lpD3DDevice->SetRenderState( D3DRENDERSTATE_ZENABLE, TRUE );
- lpD3DDevice->SetRenderState( D3DRENDERSTATE_CULLMODE, D3DCULL_CW );
- lpD3DDevice->SetRenderState( D3DRENDERSTATE_SHADEMODE, D3DSHADE_GOURAUD );
- lpD3DDevice->SetRenderState( D3DRENDERSTATE_TEXTUREPERSPECTIVE, TRUE );
- lpD3DDevice->SetRenderState( D3DRENDERSTATE_DITHERENABLE, TRUE );
-
-
- ShowCursor( FALSE );
-
- OutputDebugString( "\n\nInitialization Complete!\n\n" );
-
- return TRUE;
- }
-
- void ReleaseAll( void )
- {
- if( lpDD )
- {
- if( lpD3D )
- {
- if( lpD3DDevice )
- lpD3DDevice->Release();
-
- if( lpD3DViewport )
- lpD3DViewport->Release();
-
- lpD3D->Release();
- lpD3D = NULL;
- }
-
- if( lpDDSPrimary )
- {
- lpDDSPrimary->Release();
- lpDDSPrimary = NULL;
- lpDDSBack = NULL;
- lpDDSZ = NULL;
- }
-
- if( lpDDSBaseTex )
- {
- lpDDSBaseTex->Release();
- lpDDSBaseTex = NULL;
- }
-
- if( lpDDSBumpTex )
- {
- lpDDSBumpTex->Release();
- lpDDSBumpTex = NULL;
- }
-
- if( lpDDSEnvTex )
- {
- lpDDSEnvTex->Release();
- lpDDSEnvTex = NULL;
- }
-
- if( lpDDSTempTex )
- {
- lpDDSTempTex->Release();
- lpDDSTempTex = NULL;
- }
-
- if( lpDDSTexSystem256 )
- {
- lpDDSTexSystem256->Release();
- lpDDSTexSystem256 = NULL;
- }
-
- lpDD->Release();
- lpDD = NULL;
-
- }
-
- ShowCursor( TRUE );
- }
-
- /* --------------------------------------------------------------------------
- * Function :
- * LoadBMP
- *
- * Purpose :
- * Loads a 24-bit .bmp file onto 32-bit texture surface.
- *
- * Assumes a square texture .bmp (Size x Size)
- */
- void LoadBMP( char *filename, int Size )
- {
- int x,y;
- unsigned char r,g,b;
- BYTE *pSurface;
- DDSURFACEDESC2 ddsd;
- FILE *f;
-
- f = fopen( filename,"rb" );
- if( f == NULL )
- return;
-
- fseek( f, -Size*Size*3-2, SEEK_END );
-
- memset( &ddsd, 0, sizeof( ddsd ) );
- ddsd.dwSize = sizeof( ddsd );
-
- // Lock the surface to access it directly
- //
- switch( Size )
- {
- case 256 : lpDDSTexSystem256->Lock( NULL, &ddsd, 0, NULL ); break;
- }
-
- // Copy the bitmap data to the surface
- //
- for(y = Size-1; y >= 0; y-- )
- {
- pSurface = (BYTE *) ((BYTE *)ddsd.lpSurface + y*ddsd.lPitch);
-
- for( x = 0; x < Size; x++ )
- {
- fread( &r, 1, 1, f );
- fread( &g, 1, 1, f );
- fread( &b, 1, 1, f );
- *pSurface++ = r;
- *pSurface++ = g;
- *pSurface++ = b;
- *pSurface++ = 255; // Set alpha to anything
- }
- }
-
- fclose(f);
-
- switch( Size )
- {
- case 256 : lpDDSTexSystem256->Unlock( NULL ); break;
- }
-
- }
-
- /* --------------------------------------------------------------------------
- * Function :
- * LoadBMPToRawBuffer
- *
- * Purpose :
- * Loads a 24-bit .bmp file onto 8-bit raw buffer
- *
- * Assumes a square texture .bmp (Size x Size)
- */
- void LoadBMPToRawBuffer( char *filename, int Size, unsigned char *Buffer )
- {
- int x, y;
- unsigned char r, g, b;
- FILE *f;
-
- f = fopen( filename, "rb" );
- if( f == NULL )
- return;
-
- fseek( f, -Size*Size*3-2, SEEK_END );
-
- for( y = Size-1; y >= 0; y-- )
- {
- for( x = 0; x < Size; x++ )
- {
- fread( &r, 1, 1, f );
- fread( &g, 1, 1, f );
- fread( &b, 1, 1, f );
-
- Buffer[(y*Size+x+0)] = r;
- }
- }
-
- fclose(f);
- }
-
- /* --------------------------------------------------------------------------
- * Function :
- * CreateTexture
- *
- * Purpose :
- * Creates a texture
- *
- */
- LPDIRECT3DTEXTURE2 CreateTexture( LPDIRECTDRAWSURFACE4 *DDSTex, int TextureSize )
- {
- DDSURFACEDESC2 ddsd;
- LPDIRECT3DTEXTURE2 *t;
-
- t = (LPDIRECT3DTEXTURE2 *)malloc( sizeof(LPDIRECT3DTEXTURE2) );
-
- // Create a texture surface in video memory
- //
- memset( &ddsd, 0, sizeof(ddsd) );
- ddsd.dwSize = sizeof( DDSURFACEDESC2 );
- ddsd.dwFlags = DDSD_WIDTH
- | DDSD_HEIGHT
- | DDSD_CAPS
- | DDSD_PIXELFORMAT
- ;
-
- ddsd.dwWidth = TextureSize;
- ddsd.dwHeight = TextureSize;
- ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE
- | DDSCAPS_ALLOCONLOAD
- ;
-
- ddsd.ddpfPixelFormat.dwSize = sizeof( DDPIXELFORMAT );
- ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
- ddsd.ddpfPixelFormat.dwFourCC = 0;
- ddsd.ddpfPixelFormat.dwRGBBitCount = 32;
- ddsd.ddpfPixelFormat.dwRBitMask = 0x00FF0000;
- ddsd.ddpfPixelFormat.dwGBitMask = 0x0000FF00;
- ddsd.ddpfPixelFormat.dwBBitMask = 0x000000FF;
- ddsd.ddpfPixelFormat.dwRGBAlphaBitMask = 0;
-
- if( _3DDevice.isHAL )
- ddsd.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
- else
- ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
-
- lpDD->CreateSurface( &ddsd, DDSTex, NULL );
-
- (*DDSTex)->QueryInterface( IID_IDirect3DTexture2, (LPVOID *) t );
-
- return *t;
- }
-
- /* --------------------------------------------------------------------------
- * Function :
- * TransformVertices
- *
- * Purpose :
- * Prepare vertex data to be sent to renderer. Calculate UV's for envmap
- *
- */
- void TransformVertices( void )
- {
- // Set up vertices
- //
- Vertices[0].sx = 0.0f;
- Vertices[0].sy = 0.0f;
- Vertices[0].sz = 1.0f;
- Vertices[0].rhw = 1.0f;
- Vertices[0].tu = 0.0f;
- Vertices[0].tv = 0.0f;
-
- Vertices[1].sx = 0.0f;
- Vertices[1].sy = 255.0f;
- Vertices[1].sz = 1.0f;
- Vertices[1].rhw = 1.0f;
- Vertices[1].tu = 0.0f;
- Vertices[1].tv = 1.0f;
-
- Vertices[2].sx = 255.0f;
- Vertices[2].sy = 255.0f;
- Vertices[2].sz = 1.0f;
- Vertices[2].rhw = 1.0f;
- Vertices[2].tu = 1.0f;
- Vertices[2].tv = 1.0f;
-
- Vertices[3].sx = 255.0f;
- Vertices[3].sy = 0.0f;
- Vertices[3].sz = 1.0f;
- Vertices[3].rhw = 1.0f;
- Vertices[3].tu = 1.0f;
- Vertices[3].tv = 0.0f;
-
-
- // Calculate U,V's for the environment map
- //
- for( int i = 0; i < 4; i++ )
- {
- // Just a simple planar mapping
- // Should use transformed vertex normals to do a spherical mapping instead.
- //
- Vertices[i].tu2 = (Vertices[i].sx - WIDTH/2)/WIDTH + (float)(WIDTH-MouseX)/WIDTH;
- Vertices[i].tv2 = (Vertices[i].sy - HEIGHT/2)/HEIGHT + (float)(HEIGHT-MouseY)/HEIGHT;
- Vertices[i].color = D3DRGB(1.0f,1.0f,1.0f);
- }
-
- // Set up triangles
- //
- Triangles[0] = 0;
- Triangles[1] = 1;
- Triangles[2] = 2;
-
- Triangles[3] = 0;
- Triangles[4] = 2;
- Triangles[5] = 3;
-
- }
-
- BOOL Render( void )
- {
- HRESULT err;
- DDBLTFX ddbltfx;
-
- if( lpDDSBack->IsLost() )
- lpDDSBack->Restore();
-
- if( lpDDSZ->IsLost() )
- lpDDSZ->Restore();
-
- // This is where the rendering is taking place
-
- memset( &ddbltfx, 0, sizeof(ddbltfx) );
- ddbltfx.dwSize = sizeof( DDBLTFX );
- ddbltfx.dwFillColor = 0;
- err = lpDDSBack->Blt( NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx );
-
- memset( &ddbltfx, 0, sizeof(ddbltfx) );
- ddbltfx.dwSize = sizeof( DDBLTFX );
- ddbltfx.dwFillDepth = 0xFFFF;
- err = lpDDSZ->Blt( NULL, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &ddbltfx );
-
- // Update back buffer
- //
- err = lpD3DDevice->BeginScene();
- if(err != DD_OK)
- {
- OutputDebugString("\nBeginScene() Failed!\n");
- return FALSE;
- }
-
- lpD3DDevice->SetTextureStageState( 0, D3DTSS_ADDRESS, D3DTADDRESS_WRAP );
- lpD3DDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTFG_LINEAR );
- lpD3DDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTFN_LINEAR );
-
- lpD3DDevice->SetTextureStageState( 1, D3DTSS_ADDRESS, D3DTADDRESS_WRAP );
- lpD3DDevice->SetTextureStageState( 1, D3DTSS_MAGFILTER, D3DTFG_LINEAR );
- lpD3DDevice->SetTextureStageState( 1, D3DTSS_MINFILTER, D3DTFN_LINEAR );
-
- lpD3DDevice->SetTextureStageState( 2, D3DTSS_ADDRESS, D3DTADDRESS_WRAP );
- lpD3DDevice->SetTextureStageState( 2, D3DTSS_MAGFILTER, D3DTFG_LINEAR );
- lpD3DDevice->SetTextureStageState( 2, D3DTSS_MINFILTER, D3DTFN_LINEAR );
-
-
- // Base Texture
- //
- lpD3DDevice->SetTexture( 0, lpBaseTexture );
- lpD3DDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
- lpD3DDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
- lpD3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
- lpD3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
- lpD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
- lpD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
-
- // Bump Texture
- //
- lpD3DDevice->SetTexture( 1, lpBumpTexture );
- lpD3DDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 0 );
- lpD3DDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_BUMPENVMAP );
- lpD3DDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
- lpD3DDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_CURRENT );
- lpD3DDevice->SetTextureStageState( 1, D3DTSS_BUMPENVMAT00, FLOATtoDWORD(1.0f) );
- lpD3DDevice->SetTextureStageState( 1, D3DTSS_BUMPENVMAT01, FLOATtoDWORD(0.0f) );
- lpD3DDevice->SetTextureStageState( 1, D3DTSS_BUMPENVMAT10, FLOATtoDWORD(0.0f) );
- lpD3DDevice->SetTextureStageState( 1, D3DTSS_BUMPENVMAT11, FLOATtoDWORD(1.0f) );
- lpD3DDevice->SetTextureStageState( 1, D3DTSS_BUMPENVLSCALE, FLOATtoDWORD(1.0f) );
- lpD3DDevice->SetTextureStageState( 1, D3DTSS_BUMPENVLOFFSET, FLOATtoDWORD(0.0f) );
-
- // Environment Texture
- //
- lpD3DDevice->SetTexture( 2, lpEnvTexture );
- lpD3DDevice->SetTextureStageState( 2, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP );
- lpD3DDevice->SetTextureStageState( 2, D3DTSS_TEXCOORDINDEX, 1 );
- lpD3DDevice->SetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_ADD );
- lpD3DDevice->SetTextureStageState( 2, D3DTSS_COLORARG1, D3DTA_TEXTURE );
- lpD3DDevice->SetTextureStageState( 2, D3DTSS_COLORARG2, D3DTA_CURRENT );
-
- // Render the triangles!
- //
- err = lpD3DDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST
- , D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX2
- , (LPVOID)Vertices
- , 4
- , (LPWORD)Triangles
- , 6
- , D3DDP_DONOTCLIP
- );
- if( err != D3D_OK )
- {
- OutputDebugString( "\n\nDrawIndexedPrimitive() Failed!\n\n" );
- return FALSE;
- }
-
- if( lpD3DDevice->EndScene() != D3D_OK )
- {
- OutputDebugString( "\nEndScene() Failed!\n" );
- return FALSE;
- }
-
- // Flip
- //
- lpDDSPrimary->Flip( NULL, DDFLIP_WAIT );
-
- return TRUE;
- }
-
- void DoSomeCoolEffect( void )
- {
- int i, j;
-
- // Put some random raindrops
- for( i = 0; i < 50; i++ )
- {
- int x, y;
- x = rand() % 254 + 1;
- y = rand() % 254 + 1;
- InBuffer[ y*256 + x ] = rand() % 255;
- }
-
- // Update OutBuffer
- //
- for( i = 1; i < 255; i++ )
- {
- for( j = 1; j < 255; j++ )
- {
- int index;
-
- index = j*256 + i;
- OutBuffer[index] = (InBuffer[index-1] + InBuffer[index-256] + InBuffer[index+1] + InBuffer[index+256]) >> 2;
- }
- }
-
- }
-
- /* --------------------------------------------------------------------------
- * Function :
- * InitBumpmap
- *
- * Purpose :
- * Create a bump map texture and fill its content converted from raw RGB
- * to DX6 BUMPDUDV format
- *
- */
- void InitBumpmap( unsigned char *Buffer, int TextureSize )
- {
- // Create the bump map surface.
- //
- DDSURFACEDESC2 ddsd;
-
- // Calculate displacement map for the bumpmap
- // Fill the bits of the new texture surface with bits from a private format.
- //
- ddsd.dwSize = sizeof( ddsd );
- lpDDSBumpTex->Lock( NULL, &ddsd, DDLOCK_WAIT , NULL );
-
- for( DWORD y = 0; y < ddsd.dwHeight; y++ )
- {
- BYTE* pDst = (BYTE*)ddsd.lpSurface + y*ddsd.lPitch;
-
- for( DWORD x = 0; x < ddsd.dwWidth; x++ )
- {
- LONG v00, v01, v10;
-
- v00 = Buffer[(y*TextureSize+x)]; // Get the current pixel
-
- if( x == ddsd.dwWidth-1 )
- v01 = Buffer[(y*TextureSize+x)]; // and the pixel to the right
- else
- v01 = Buffer[(y*TextureSize+x+1)]; // and the pixel to the right
-
- if( y == ddsd.dwHeight-1 )
- v10 = Buffer[((y)*TextureSize+x)]; // and the pixel one line below.
- else
- v10 = Buffer[((y+1)*TextureSize+x)]; // and the pixel one line below.
-
- LONG iDu = v00 - v01; // The delta-u bump value
- LONG iDv = v00 - v10; // The delta-v bump value
-
- *pDst++ = (BYTE)iDu;
- *pDst++ = (BYTE)iDv;
- }
-
- }
-
- lpDDSBumpTex->Unlock(0);
- }
-
- /* --------------------------------------------------------------------------
- * Function :
- * WinMain()
- *
- * Purpose :
- * Program entry point.
- */
- int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
- {
- MSG msg;
- BOOL err;
- int FrameNo = 0;
-
- // Initialize D3D
- //
- if( !Init() )
- {
- ReleaseAll();
- OutputDebugString("\n\nError occured while initializing! :(\n\n");
- return -1;
- }
-
- // Create all textures needed by the application
- //
- lpBaseTexture = CreateTexture( &lpDDSBaseTex, 256 );
- LoadBMP( BASEMAPFILENAME, 256 );
- lpBaseTexture->Load( lpTexSystem256 );
-
- // Load the bump map file to a buffer
- //
- Bumpdata = (unsigned char *)malloc( 256*256 );
- Bumpdata2 = (unsigned char *)malloc( 256*256 );
- MatroxLogo = (unsigned char *)malloc( 256*256 );
-
- LoadBMPToRawBuffer( BUMPMAPFILENAME, 256 , MatroxLogo );
-
- // Convert the RGB (assumes a grayscale image) to DX6 Bump format
- //
- memset( Bumpdata, 0, 256*256 );
- memset( Bumpdata2, 0, 256*256 );
-
- InitBumpmap( Bumpdata, 256 );
-
- // Environment map texture
- //
- lpEnvTexture = CreateTexture( &lpDDSEnvTex, 256 );
- LoadBMP( ENVMAPFILENAME, 256 );
- lpEnvTexture->Load( lpTexSystem256 );
-
-
- InBuffer = Bumpdata;
- OutBuffer = Bumpdata2;
-
- // Windoze message pump
- //
- while( TRUE )
- {
- if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
- {
- // Message pending. If it's QUIT then exit the message loop.
- if (WM_QUIT == msg.message)
- {
- break;
- }
-
- // Otherwise, process the message.
- else
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- else
- {
- // Render the scene
- //
- TransformVertices();
-
- FrameNo++;
- if( FrameNo > 200 )
- {
- FrameNo = 0;
-
- // Copy the Matrox logo to the InBuffer
-
- for( int i = 0; i < 256; i++ )
- {
- for( int j = 0; j < 256; j++ )
- {
- unsigned char pixel;
- int index;
-
- index = i*256+j;
- pixel = MatroxLogo[index];
-
- if( pixel )
- InBuffer[index] = pixel;
- }
- }
- }
-
- // Do Some Cool Effect
- if( DoEffect )
- DoSomeCoolEffect();
- else
- memcpy( OutBuffer, MatroxLogo, 256*256 );
- InitBumpmap( OutBuffer, 256 );
-
- // Swap the buffers used for the rain effect
- //
- unsigned char *temp;
- temp = InBuffer;
- InBuffer = OutBuffer;
- OutBuffer = temp;
-
- err = Render();
-
- if( err == FALSE )
- goto End;
- }
- }
-
- End:
- ReleaseAll();
-
- return 0;
- }
-